package gray.android;

import java.math.BigDecimal;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;
import gray.Factory;
import gray.Field;
import gray.Frame;
import snippet.Snipet;
import gray.android.button.BButton;
import gray.android.elem.BigTextElem;
import gray.android.elem.TextElem;
import gray.android.elem.TitleElem;
import gray.android.field.AdresseField;
import gray.android.field.BigTextField;
import gray.android.field.CheckField;
import gray.android.field.CpField;
import gray.android.field.DateField;
import gray.android.field.EmailField;
import gray.android.field.IntegerField;

import gray.android.field.MoneyField;
import gray.android.field.NameField;
import gray.android.field.PercentField;
import gray.android.field.RadioField;
import gray.android.field.TelephoneField;
import gray.android.field.TextField;
import gray.android.field.TimeField;
import gray.android.field.UrlField;
import gray.android.frame.RawFrame;
import util.observer.Trigger;
import snippet.SnipetEN;

public class FactoryAndroid implements Factory<View>{
	private final Context _context;
	private final Activity _activity;
  private final Snipet _lang;
  
	public FactoryAndroid(Activity activity, final Snipet lang){
		_context = activity.getApplicationContext();
		_activity = activity;
    if (lang==null)
      _lang=SnipetEN.singleton();
    else
      _lang=lang;
	}
	
	public static void message(final Context context, final String msg){
		Toast.makeText(context, msg, 10).show();
	}
	
	
	@Override
	public void swap(Frame<View> f) {
		ScrollView sv = new ScrollView(_context);
		sv.addView(f.container());
    sv.setBackgroundColor(0xFFB4B4B4);
		_activity.setContentView(sv);
	}

	@Override
	public Frame<View> button(Trigger observer, String name, Character key) {
		return BButton.factoryValideWithMsg(_context, observer, name, key, null, null);

	}

	@Override
	public Frame<View> buttonWithMsg(Trigger observer, String name,
			Character key, Object msg) {
		return BButton.factoryValideWithMsg(_context, observer, name, key, msg, null);
	}

	@Override
	public Frame<View> buttonValid(Trigger observer, String name,
			Character key, Frame<View> valid) {
		return BButton.factoryValideWithMsg(_context, observer, name, key, null, valid);
	}

	@Override
	public Frame<View> buttonValidWithMsg(Trigger observer, String name,
			Character key, Object msg, Frame<View> valid) {
		return BButton.factoryValideWithMsg(_context, observer, name, key, msg, valid);
	}

	//elem
	@Override
	public Frame<View> elemDate(Date date) {
		return TextElem.factoryDate(_context, date);
	}

	@Override
	public Frame<View> elemMoney(BigDecimal num) {
		return TextElem.factoryMoney(_context, num);
	}

	@Override
	public Frame<View> elemText(String name) {
		return TextElem.factory(_context, name);
	}

	@Override
	public Frame<View> elemTime(Date hour) {
		return TextElem.factoryTime(_context, hour);
	}

	@Override
	public Frame<View> elemString(String num) {
		return TextElem.factoryString(_context, num);
	}

	@Override
	public Frame<View> elemPercentage(final BigDecimal num) {
		return TextElem.factoryPercentage(_context,num);
	}

	//FRAME
	@Override
	public Frame<View> frameBottom(final Frame<View> _bottom, final Frame<View> _rest) {
		//throw new UnsupportedOperationException();
    return frameRaw(new Frame[]{_rest, _bottom});
	}

	@Override
	public Frame<View> frameTop(final Frame<View> _top, final Frame<View> _rest) {
		//throw new UnsupportedOperationException();
    return frameRaw(new Frame[]{_top, _rest});
	}

	@Override
	public Frame<View> frameLeft(final Frame<View> _left, final Frame<View> _rest) {
		//throw new UnsupportedOperationException();
    return frameRaw(new Frame[]{_left, _rest});
	}

	@Override
	public Frame<View> frameRight(final Frame<View> _right, final Frame<View> _rest) {
		//throw new UnsupportedOperationException();
    return frameRaw(new Frame[]{_rest, _right});
	}

	@Override
	public Frame<View> frameList(final Frame<View>... lst) {
		//throw new UnsupportedOperationException();
    return frameRaw(lst);
	}

	@Override
	public Frame<View> frameSplit(final Frame<View> left, final Frame<View> right) {
		//throw new UnsupportedOperationException();
    return frameRaw(new Frame[]{left, right});
	}

	@Override
	public Frame<View> frameRaw(final Frame<View>... lst) {
		return RawFrame.factory(_context, lst);
	}
	//FIELD
	@Override
	public Field<String, View> fieldBigText(final String name, final Character key) {
		return BigTextField.factory(_context, name,'c');
	}

	@Override
	public Field<String, View> fieldComboBox(String name, String[] lst, Character key) {
		throw new UnsupportedOperationException();
		//return null;
	}

	@Override
	public Field<Date, View> fieldDate(String name, Character key) {
		return DateField.factory(_context, _lang, name, key);
	}

	@Override
	public Field<BigDecimal, View> fieldMoney(String name, Character key) {
		return MoneyField.factory(_context, _lang, name, key);
	}

	@Override
	public <E extends Enum<E>> Field<Enum<E>, View> fieldRadio(String title,
			Class<E> enumData, final Character[] key) {
		return RadioField.factory(_context, title, enumData);
	}

	@Override
	public Field<String, View> fieldText(String name, Character key) {
		return TextField.factory(_context, name, key);
	}

	@Override
	public Field<Date, View> fieldTime(String name, Character key) {
		return TimeField.factory(_context, _lang, name, key);
	}

	@Override
	public Field<BigDecimal, View> fieldPercentage(String name, Character key) {
		return PercentField.factory(_context, _lang, name, key);
	}

	@Override
	public Field<Integer, View> fieldInteger(String name, Character key) {
		return IntegerField.factory(_context, _lang, name, key);
	}
	//REQUIRED FIELD
	@Override
	public Field<Date, View> fieldDateRequired(String name, Character key) {
		return DateField.factoryRequired(_context, _lang, name, key);
	}

	@Override
	public Field<BigDecimal, View> fieldMoneyRequired(String name, Character key) {
		return MoneyField.factoryRequired(_context, _lang, name, key);
	}

	@Override
	public Field<String, View> fieldTextRequired(String name, Character key) {
		return TextField.factoryRequired(_context, name, key);
	}

	@Override
	public Field<Date, View> fieldTimeRequired(String name, Character key) {
		return TimeField.factoryRequired(_context, _lang, name, key);
	}

	@Override
	public Field<BigDecimal, View> fieldPercentageRequired(String name, Character key) {
		return PercentField.factoryRequired(_context, _lang, name, key);
	}

	@Override
	public Field<Integer, View> fieldIntegerRequired(String name, Character key) {
		return IntegerField.factoryRequired(_context, _lang, name, key);
	}

  @Override
  public Frame<View> elemBigText(String txt) {
    return BigTextElem.factory(_context, txt);
  }

  public Frame<View> elemTitle(String txt) {
    return TitleElem.factory(_context,  txt);
  }

  public Field<Boolean, View> fieldCheckBox(String name, Character key) {
    return CheckField.factory(_context,  name, key);
  }

  public Field<String, View> fieldName(String name, Character key) {
    return NameField.factory(_context,  _lang, name, key);
  }

  public Field<String, View> fieldAdresse(String name, Character key) {
    return AdresseField.factory(_context,  _lang, name, key);
  }

  public Field<String, View> fieldCP(String name, Character key) {
    return CpField.factory(_context,  _lang, name, key);
  }

  public Field<String, View> fieldTelephone(String name, Character key) {
    return TelephoneField.factory(_context,  _lang, name, key);
  }

  public Field<String, View> fieldUrl(String name, Character key) {
    return UrlField.factory(_context,  _lang, name, key);
  }

  public Field<String, View> fieldEmail(String name, Character key) {
    return EmailField.factory(_context,  _lang, name, key);
  }

  public Field<String, View> fieldNameRequired(String name, Character key) {
    return NameField.factoryRequired(_context,  _lang, name, key);
  }

  public Field<String, View> fieldAdresseRequired(String name, Character key) {
    return AdresseField.factoryRequired(_context,  _lang, name, key);
  }

  public Field<String, View> fieldCPRequired(String name, Character key) {
    return CpField.factoryRequired(_context,  _lang, name, key);
  }

  public Field<String, View> fieldTelephoneRequired(String name, Character key) {
    return TelephoneField.factoryRequired(_context,  _lang, name, key);
  }

  public Field<String, View> fieldUrlRequired(String name, Character key) {
    return UrlField.factoryRequired(_context,  _lang, name, key);
  }

  public Field<String, View> fieldEmailRequired(String name, Character key) {
    return EmailField.factoryRequired(_context,  _lang, name, key);
  }
}
